//
// Created by tys on 2017/10/26.
//

#ifndef MYPLAYER_AUDIOMEDIA_H
#define MYPLAYER_AUDIOMEDIA_H


#include <queue>
#include <SLES/OpenSLES.h>
#include <SLES/OpenSLES_Android.h>
#include "MultiMedia.h"

class AudioMedia : public MultiMedia {

public:
    AudioMedia();

    virtual ~AudioMedia();

    pthread_cond_t cond;

    pthread_mutex_t mutex;

    /**是否在播放，是否消费packet*/
    bool isPlay;

    std::queue<AVPacket *> queue;

    /**转换器上下文*/
    SwrContext *swr_ctx = NULL;

    //输出采样率(等于输入采样率)
    int dst_rate = 44100;
    //输出通道layout
    int64_t dst_ch_layout = AV_CH_LAYOUT_STEREO;
    AVSampleFormat dst_sample_fmt = AV_SAMPLE_FMT_S16;
    uint8_t *out_buf = NULL;
    int dst_ch_nb;

    /**
     * 队列里面放入音频压缩数据
     * */
    void put(AVPacket *packet);

    void get(AVPacket **packet);

    void setIsPlay(bool isPlay);

    /**
     * 取出音频数据进行加压缩播放
     * */
    void play();

    /**初始化opensl es*/
    int initOpenSlEs();

    /**初始化ffmpeg swr_ctx*/
    int initSwrCtx();

    SLEffectSendItf bqPlayerEffectSend;
    SLAndroidSimpleBufferQueueItf bqPlayerBufferQueue;
private:

    SLObjectItf engineObject;
    SLEngineItf engineEngine;
    SLEnvironmentalReverbItf outputMixEnvironmentalReverb;
    SLObjectItf outputMixObject;
    SLObjectItf bqPlayerObject;
    SLVolumeItf bqPlayerVolume;
    SLPlayItf bqPlayerPlay;
    pthread_t thread_t_audio;
};


#endif //MYPLAYER_AUDIOMEDIA_H
